package com.margo.project.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.http.entity.ContentProducer;
import org.apache.http.util.Args;

/**
 * 重写writeTo方法
 * @author fxb208
 *
 */
public class ContentProducerImpl implements ContentProducer{

	private File file;
	
	
	public ContentProducerImpl() {
		super();
	}

	public ContentProducerImpl(File file) {
		super();
		this.file = file;
	}

	public void writeTo(OutputStream outstream) throws IOException {
	       Args.notNull(outstream, "Output stream");
	        final InputStream instream = new FileInputStream(file);
	        try {
	            int l;
	            final byte[] tmp = new byte[1024];
	            while ((l = instream.read(tmp)) != -1) {
	                outstream.write(tmp, 0, l);
	            }
	        } finally {
	            instream.close();
	        }
		
	}

}
